@extends('layouts.admin')
@section('pageTitle', '触发列表')
@section('content')
    <div class="main-panel">
        <div class="content apartment-index">
            <div class="container-fluid" style="min-height:600px;">
                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="header">
                                <legend>触发列表</legend>
                            </div>
                            <div class="toolbar">
                                <button onclick="window.location.href='/web/activityTrigger/add'" class="btn btn-success" id="create_point" >
                                    <i class="fa fa-plus"></i> 新建规则
                                </button>
                            </div>

                            <table id="bootstrap-table-container" class="table" data-url="/web/activityTrigger/list" data-delete-url="/web/activityTrigger/delete">
                                <thead>
                                <th data-field="state" data-checkbox="true"></th>
                                <th data-field="weight" data-sortable="true">优先级</th>
                                <th data-field="rule_name" data-sortable="true">规则名称</th>
                                <th data-field="act_id"  data-sortable="true" data-formatter="actNameFormatter">关联活动</th>
                                <th data-field="platform" data-sortable="false" data-formatter="platformFormatter">投放平台</th>
                                <th data-field="created_at"  data-sortable="false">创建时间</th>
                                <th data-field="status" data-sortable="false" data-formatter="statusFormatter">状态</th>
                                <th data-field="actions" class="td-actions text-center" data-events="operateEvents" data-formatter="operateFormatter">操作</th>
                                </thead>
                                <tbody>

                                </tbody>
                            </table>
                        </div><!--  end card  -->
                    </div> <!-- end col-md-12 -->
                </div> <!-- end row -->
            </div>
        </div>
    </div>
@endsection
@section('script')
    <script>
        function deliveryTime(value,row) {
           return row.begin_time +' - '+row.end_time;
        }
        function actNameFormatter(value,row) {
            return row.act_name;
        }
        function platformFormatter(value,row) {
            var str = '';
            var num_JS = parseInt("{{\App\Models\ActivityTrigger\ActivityTrigger::ACTIVITY_TRIGGER_PLATFORM_JS}}");
            var num_IOS = parseInt("{{\App\Models\ActivityTrigger\ActivityTrigger::ACTIVITY_TRIGGER_PLATFORM_IOS}}");
            var num_Android = parseInt("{{\App\Models\ActivityTrigger\ActivityTrigger::ACTIVITY_TRIGGER_PLATFORM_ANDROID}}");

            if ((row.platform & num_JS) == num_JS){
                str += '<span>JS </span>';
            }
            if((row.platform & num_IOS) == num_IOS){
                str += '<span>Ios </span>';
            }
            if((row.platform & num_Android) == num_Android){
                str += '<span>Android </span>';
            }
            return str;
        }
        function statusFormatter(value,row) {
            var str = '';
            if (row.status == "{{\App\Models\ActivityTrigger\ActivityTrigger::ACTIVITY_TRIGGER_STATUS_PAUSE}}"){
                str = '<span class="text-warning">已暂停</span>';
            }
            if(row.status == "{{\App\Models\ActivityTrigger\ActivityTrigger::ACTIVITY_TRIGGER_STATUS_START}}"){
                str = '<span class="">进行中</span>';
            }
            return str;
        }

        function operateFormatter(value,row,index) {
            var dropdown_id = 'dropdown_'+new Date().getTime();
            var actions = [];
            actions.push( [
                '<a rel="tooltip" title="编辑" class="btn btn-simple btn-warning btn-icon table-action edit" href="javascript:void(0)">',
                '<i class="fa fa-edit"></i>',
                '</a>',
                '<div class="btn-group"><a  id="'+dropdown_id+'" aria-haspopup="true"  aria-expanded="false" data-toggle="dropdown" class="btn btn-simple btn-danger btn-icon table-action dropdown-toggle more" href="javascript:void(0)">',
                '<i class="fa fa-ellipsis-h"></i>',
                '</a>',
                '<ul class="dropdown-menu td-actions-dropdown" role="menu" aria-labelledby="'+dropdown_id+'">',
                '<li><a href="#" class="pause">'+(row.status == parseInt("{{\App\Models\ActivityTrigger\ActivityTrigger::ACTIVITY_TRIGGER_STATUS_PAUSE}}") ? '开始':'暂停')+'</a></li>',
                '<li><a href="#" class="delete">删除</a></li>',
                '</ul>' ,
                '</div>'
            ].join(''));
            return actions.join('');
        }
        window.operateEvents = {
            'click .pause':function (e,value,row) {
                var status_pause =parseInt("{{\App\Models\ActivityTrigger\ActivityTrigger::ACTIVITY_TRIGGER_STATUS_PAUSE}}");
                var status_start =parseInt("{{\App\Models\ActivityTrigger\ActivityTrigger::ACTIVITY_TRIGGER_STATUS_START}}");
                var status = ( row.status == status_start )?status_pause:status_start;
                $.post('/web/activityTrigger/edit',{id:row.id,status:status},function (res) {
                    if (res.code == 0){
                        utils.showNotification('操作成功',2);
                        $('#bootstrap-table-container').bootstrapTable('refresh');
                    }else {
                        utils.showNotification('操作失败',3);
                    }
                })
            },
            'click .delete':function (e,value,row) {
                var bt = document.body.querySelector('#bootstrap-table-container');
                var event = new CustomEvent("table-delete-item", { "detail": [row.id] });
                bt.dispatchEvent(event);
            },
            'click .edit':function (e,value,row) {
                window.location.href = '/web/activityTrigger/editActivityTrigger?id='+row.id;
            }
        }
    </script>
@endsection